#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：qianxun_api 
@File    ：conftest.py
@IDE     ：PyCharm 
@Author  ：future76_lly
@Date    ：2023/12/18 11:21 
'''
# 标准库导入
import logging
from enum import unique, Enum
# 第三方库导入
import pytest
# 本地应用/模块导入
from loguru import logger
from common.path_config import failed_tests


# 当执行一个case的时候会自动的调用这个方法：把对应的数据传过来给到call
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    # 通过 out = yield 定义了一个生成器。在生成器中，res = out.get_result() 获取了测试结果对象。
    out = yield
    res = out.get_result()
    #  res.when == "call"：表示正在运行调用测试函数的阶段。
    if res.when == "call":
        logging.info(f"用例ID：{res.nodeid}")
        logging.info(f"测试结果：{res.outcome}")
        logging.info(f"故障表示：{res.longrepr}")
        logging.info(f"异常：{call.excinfo}")
        logging.info(f"用例耗时：{res.duration}")
        logging.info("**************************************")

@unique
class AssertMethod(Enum):
    """
    断言类型
    注意：这里的类型与assert_function.py中的方法名相同，不要随意改动
    """
    equals = "eq"
    less_than = "lt"
    less_than_or_equals = "le"
    greater_than = "gt"
    greater_than_or_equals = "ge"
    not_equals = "not_eq"
    string_equals = "str_eq"
    length_equals = "len_eq"
    length_greater_than = "len_gt"
    length_greater_than_or_equals = 'len_ge'
    length_less_than = "len_lt"
    length_less_than_or_equals = 'len_le'
    contains = "contains"
    contained_by = 'contained_by'
    startswith = 'startswith'
    endswith = 'endswith'
    one_to_many = 'one_to_many'

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    # execute all other hooks to obtain the report object
    outcome = yield
    rep = outcome.get_result()

    if rep.when == "call" and rep.failed:
        func = item.callspec
        # 获取测试用例的参数
        params = func.params
        logger.error('\n当前执行失败的测试用例是：{}'.format(params['CaseData']['caseName']))
        failed_tests.append(params['CaseData']['caseName'])
        logger.info("\n当前用例执行完成--------------------------------------------------------\n\n\n")
